package com.javagroup.chessonline.client;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;

import com.javagroup.chessonline.chessman.ChessData;
import com.javagroup.chessonline.chessman.Chessman;





public class ClientWorker implements Runnable  {
	PrintWriter pw;
	BufferedReader br;
	Socket clientSocket;
	Client client;
	ObjectOutputStream oos;
	ObjectInputStream ois;

	public ClientWorker(Socket clientSocket,Client client) throws IOException {
		this.clientSocket = clientSocket;
		this.client = client;
		pw = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
		br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
		oos = new ObjectOutputStream(clientSocket.getOutputStream());
		ois = new ObjectInputStream(clientSocket.getInputStream());
		oos.flush();
		//pw.println("hello server");
		//pw.flush();
		System.out.println("Da ket noi den server");
	}

	public void send(String s){
		pw.println(s);
		pw.flush();
		
	}
	public String get(){
		String s = null;
		try {
			s =  br.readLine();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return s;
	}
	public void sendObject (Object obj){
		try {
			//oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.writeObject(obj);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Object receiveObject (){
		Object obj = null;
		try {
			//ois = new ObjectInputStream(clientSocket.getInputStream());
			obj = ois.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
	public void sendArrayChess (Chessman[] arrayChess){
		Chessman[] obj = arrayChess;
		try {
			//oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.writeObject(obj);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Chessman[] receiveArrayChess (){
		Chessman[] arrayChess = null;
		try {
		/*	if(ois == null){
				ois = new ObjectInputStream(clientSocket.getInputStream());
			}*/
			
			arrayChess = (Chessman[]) ois.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayChess;
	}
	
	@Override
	public void run() {
		try {
			clientLoop();
		} catch (Exception e) {
			e.printStackTrace();
		} /*finally {
			try {
				clientSocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
	}
	/*private void clientLoop() throws IOException {
		while (true) {
			String line;
			if ((line = get()) != null) {
				
				if(line.equals("Your team is A")){
					client.loadTeamA(client);
				}
				if(line.equals("Your team is B")){
					client.loadTeamB(client);
				}
				if(line.equals("Current turn")){
        			int currentTurn = Integer.parseInt(get());
        			System.out.println("turn hien tai la " + currentTurn);
        			client.setCurrentTurn(currentTurn);
        		}
				if(line.equals("Refresh board")){
					ChessData data = (ChessData) receiveObject();
					if(data != null){
						client.refreshBoard(data);
						//System.out.println("mang nhan duoc la "+arrayChess.length);
						send("Get new turn");
					}
				}
				//System.out.println(line);
			}
		}
	}*/
	private void clientLoop() throws IOException {
		while (true) {
			ChessData data;
			if ((data = (ChessData) receiveObject()) != null) {
				
				if(data.getHeader().equals("Your team is A")){
					client.loadTeamA(client);
				}
				if(data.getHeader().equals("Your team is B")){
					client.loadTeamB(client);
				}
				if(data.getHeader().equals("Current turn")){
        			int currentTurn = data.getValue();
        			System.out.println("turn hien tai la " + currentTurn);
        			client.setCurrentTurn(currentTurn);
        		}
				if(data.getHeader().equals("Refresh board")){
					Chessman[] chess = data.getArrayChess();
					if(chess != null){
						client.refreshBoard(chess);
						//System.out.println("mang nhan duoc la "+arrayChess.length);
						//send("Get new turn");
						sendObject(new ChessData("Get new turn"));
					}
				}
			}
		}
	}
	

}
